package com.android.boncen.commonFrame;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.boncen.commonFrame.bean.ConsultationBean;
import com.android.boncen.commonFrame.bean.UserBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 功能描述:发布咨询
 *
 * @date 2017/2/10
 */

public class AddConsultationActivity extends FragmentActivity {

    @BindView(R.id.etTitle)
    EditText etTitle;
    @BindView(R.id.etContent)
    EditText etContent;
    private ProgressDialog waitDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ButterKnife.bind(this);
        waitDialog = new ProgressDialog(this);
        waitDialog.setMessage("正在处理");
    }


    private void save(String title, String content) {
        UserBean user = BmobUser.getCurrentUser(UserBean.class);
        ConsultationBean c = new ConsultationBean();
        c.setUserId(user.getObjectId());
        c.setTitle(title);
        c.setContent(content);
        c.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                waitDialog.dismiss();
                if (e == null) {
                    Toast.makeText(AddConsultationActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                } else {
                    Toast.makeText(AddConsultationActivity.this, "保存数据失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @OnClick({R.id.ib_back, R.id.btSave})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                finish();
                break;
            case R.id.btSave:
                String title = etTitle.getText().toString();

                if (TextUtils.isEmpty(title)) {
                    Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                String content = etContent.getText().toString();

                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                waitDialog.show();
                save(title, content);
                break;
        }
    }
}
